
#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "rtc_misc.h"
#include "track_service.h"

void track_update_name_text(void *content, void *obj, void *data);
void track_update_compress_text(void *content, void *obj, void *data);
void track_update_threshold_text(void *content, void *obj, void *data);
void *track_create_track_line(void *content, void *points, uint32_t cnt);
void *track_create_compress_text(void *content, void *param);
void *track_create_dots_text(void *content, void *param);
void *track_create_threshold_text(void *content, void *param);
void track_update_dots_text(void *content, void *obj, void *data);
void *track_create_track_name(void *content, void *param);
uint32_t track_load(lv_point_t **lv_points, track_payload_t **track, uint32_t threshold);

/**
 * @description:
 * @param {void} *content
 * @param {void} *data
 * @return {*}
 */
void track_update_name_text(void *content, void *obj, void *data)
{
    char *value = (char *)data;
    lv_label_set_text_fmt(obj, "%s", value);
    lv_obj_align(obj, content, LV_ALIGN_IN_TOP_MID, 0, 10);
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *data
 * @return {*}
 */
void track_update_compress_text(void *content, void *obj, void *data)
{
    uint32_t value = (uint32_t)(*((float *)data));
    lv_label_set_text_fmt(obj, "compressed : %d%%", value);
    lv_obj_align(obj, content, LV_ALIGN_IN_TOP_LEFT, 10, 48);
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *data
 * @return {*}
 */
void track_update_dots_text(void *content, void *obj, void *data)
{
    uint32_t value = *((uint32_t *)data);
    lv_label_set_text_fmt(obj, "dots : %d", value);
    lv_obj_align(obj, content, LV_ALIGN_IN_TOP_LEFT, 10, 48 + 24 + 4);
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *data
 * @return {*}
 */
void track_update_threshold_text(void *content, void *obj, void *data)
{
    uint32_t value = *((uint32_t *)data);
    lv_label_set_text_fmt(obj, "threshold : %d", value);
    lv_obj_align(obj, content, LV_ALIGN_IN_TOP_LEFT, 10, 48 + 24 * 2 + 4);
}

/**
 * @description:
 * @param {lv_point_t} *
 * @param {track_payload_t} *
 * @return {*}
 */
uint32_t track_load(lv_point_t **lv_points, track_payload_t **track, uint32_t threshold)
{
    display_area_t display_info =
    {
        .x = 40,
        .y = 40,
        .w = LV_HOR_RES_MAX - 40 *2,
        .h = LV_HOR_RES_MAX - 40 *2,
    };

    #if CFG_TRACK_IMPORT_FROM_GPX
    track_payload_t *payload = sport_track_extract_from_gpx(NULL, &display_info, "/home/billy/study/freeWear/middleware/analyze-gpx/src/gpx/example/5.gpx");
    #else
    track_payload_t *payload = track_extract_from_record(0, NULL, &display_info, threshold);
    #endif

    uint32_t cnt = payload->cnt;
    lv_point_t *line = gpx_malloc(sizeof(lv_point_t) * cnt);

    for(uint32_t i = 0; i < cnt; i++)
    {
        line[i].x = payload->array[i].x;
        line[i].y = payload->array[i].y;
    }

    *lv_points = line;
    *track = payload;

    return payload->cnt;
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *param
 * @return {*}
 */
void *track_create_track_name(void *content, void *param)
{
    track_payload_t *track = (track_payload_t *)param;

    static lv_style_t nameStyle;
    lv_style_init(&nameStyle);

    lv_style_set_pad_top(&nameStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_bottom(&nameStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_left(&nameStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_right(&nameStyle, LV_STATE_DEFAULT, 10);

    lv_style_set_text_letter_space(&nameStyle, LV_STATE_DEFAULT, 0);
    lv_style_set_text_line_space(&nameStyle, LV_STATE_DEFAULT, 20);
    lv_style_set_text_decor(&nameStyle, LV_STATE_DEFAULT, LV_TEXT_DECOR_NONE);

    lv_style_set_text_font(&nameStyle, LV_STATE_DEFAULT, &lv_font_montserrat_28);
    lv_style_set_text_color(&nameStyle, LV_STATE_DEFAULT, lv_color_hex(0xFF0000));

    lv_obj_t *nameText = lv_label_create(content, NULL);
    lv_obj_add_style(nameText, LV_LABEL_PART_MAIN, &nameStyle);

    if (track)
    {
        track_update_name_text(content, nameText, track->name);
    }

    return nameText;
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *points
 * @param {uint32_t} cnt
 * @return {*}
 */
void *track_create_track_line(void *content, void *points, uint32_t cnt)
{
    lv_point_t *lv_points = (lv_point_t *)points;

    static lv_style_t lineStyle;
    lv_style_init(&lineStyle);

    lv_style_set_line_color(&lineStyle, LV_STATE_DEFAULT, LV_COLOR_GREEN);
    lv_style_set_line_width(&lineStyle, LV_STATE_DEFAULT, 1);
    lv_style_set_line_rounded(&lineStyle, LV_STATE_DEFAULT, true);

    lv_obj_t *lv_line = lv_line_create(content, NULL);
    lv_obj_set_size(lv_line, LV_HOR_RES_MAX, LV_HOR_RES_MAX);
    lv_obj_add_style(lv_line, LV_LINE_PART_MAIN, &lineStyle);

    lv_line_set_points(lv_line, lv_points, cnt);
    lv_obj_align(lv_line, NULL, LV_ALIGN_CENTER, 0, 0);

    return lv_line;
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *param
 * @return {*}
 */
void *track_create_compress_text(void *content, void *param)
{
    track_payload_t *track = (track_payload_t *)param;

    static lv_style_t compressStyle;
    lv_style_init(&compressStyle);

    lv_style_set_pad_top(&compressStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_bottom(&compressStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_left(&compressStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_right(&compressStyle, LV_STATE_DEFAULT, 10);

    lv_style_set_text_letter_space(&compressStyle, LV_STATE_DEFAULT, 0);
    lv_style_set_text_line_space(&compressStyle, LV_STATE_DEFAULT, 20);
    lv_style_set_text_decor(&compressStyle, LV_STATE_DEFAULT, LV_TEXT_DECOR_NONE);

    lv_style_set_text_font(&compressStyle, LV_STATE_DEFAULT, &lv_font_montserrat_24);
    lv_style_set_text_color(&compressStyle, LV_STATE_DEFAULT, lv_color_hex(0x000000));

    lv_obj_t *compressText = lv_label_create(content, NULL);
    lv_obj_add_style(compressText, LV_LABEL_PART_MAIN, &compressStyle);

    if (track)
    {
        track_update_compress_text(content, compressText, &track->compress);
    }

    return compressText;
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *param
 * @return {*}
 */
void *track_create_dots_text(void *content, void *param)
{
    track_payload_t *track = (track_payload_t *)param;

    static lv_style_t compressStyle;
    lv_style_init(&compressStyle);

    lv_style_set_pad_top(&compressStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_bottom(&compressStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_left(&compressStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_right(&compressStyle, LV_STATE_DEFAULT, 10);

    lv_style_set_text_letter_space(&compressStyle, LV_STATE_DEFAULT, 0);
    lv_style_set_text_line_space(&compressStyle, LV_STATE_DEFAULT, 20);
    lv_style_set_text_decor(&compressStyle, LV_STATE_DEFAULT, LV_TEXT_DECOR_NONE);

    lv_style_set_text_font(&compressStyle, LV_STATE_DEFAULT, &lv_font_montserrat_24);
    lv_style_set_text_color(&compressStyle, LV_STATE_DEFAULT, lv_color_hex(0x000000));

    lv_obj_t *countText = lv_label_create(content, NULL);
    lv_obj_add_style(countText, LV_LABEL_PART_MAIN, &compressStyle);

    if (track)
    {
        track_update_dots_text(content, countText, &track->cnt);
    }

    return countText;
}


/**
 * @description:
 * @param {void} *content
 * @param {void} *param
 * @return {*}
 */
void *track_create_threshold_text(void *content, void *param)
{
    track_payload_t *track = (track_payload_t *)param;

    static lv_style_t thresholdStyle;
    lv_style_init(&thresholdStyle);

    lv_style_set_pad_top(&thresholdStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_bottom(&thresholdStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_left(&thresholdStyle, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_right(&thresholdStyle, LV_STATE_DEFAULT, 10);

    lv_style_set_text_letter_space(&thresholdStyle, LV_STATE_DEFAULT, 0);
    lv_style_set_text_line_space(&thresholdStyle, LV_STATE_DEFAULT, 20);
    lv_style_set_text_decor(&thresholdStyle, LV_STATE_DEFAULT, LV_TEXT_DECOR_NONE);

    lv_style_set_text_font(&thresholdStyle, LV_STATE_DEFAULT, &lv_font_montserrat_24);
    lv_style_set_text_color(&thresholdStyle, LV_STATE_DEFAULT, lv_color_hex(0x000000));

    lv_obj_t *thresholdText = lv_label_create(content, NULL);
    lv_obj_add_style(thresholdText, LV_LABEL_PART_MAIN, &thresholdStyle);

    if (track)
    {
        track_update_threshold_text(content, thresholdText, &track->threshold);
    }

    return thresholdText;
}
